<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 留言
                </el-breadcrumb-item>
                <el-breadcrumb-item>我要留言</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="form" :model="msgform" label-width="80px">
                    <el-form-item label="留言主题">
                        <el-input v-model="param.title"></el-input>
                    </el-form-item>
                    <el-form-item label="接收人">
                        <el-select v-model="value" filterable placeholder="请选择">
                            <el-option
                                    v-for="item in options"
                                    :key="item.userid"
                                    :label="item.username"
                                    :value="item.userid">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="留言内容">
                        <el-input type="textarea" rows="5" v-model="param.content"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">表单提交</el-button>
                        <el-button>取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import {getReceiverList, addMsg} from "../../api/admin";

    export default {
        name: 'baseform',
        data() {
            return {
                options: [{
                    userid: '',
                    username: ''
                }],
                value: '',
                msgform: {
                    name: '',
                    region: '',
                    date1: '',
                    date2: '',
                    delivery: true,
                    type: ['步步高'],
                    resource: '小天才',
                    desc: '',
                    options: []
                },
                param: {
                    title:'',
                    value:'',
                    content:'',

                },
                query: {
                    userid:''
                }
            };
        },
        created() {
          this.getData();
        },
        methods: {
            getData() {
                let user1 = JSON.parse(localStorage.getItem('user'));
                this.query.userid=user1.userid;
                getReceiverList(this.query).then(res => {
                    console.log(res);
                    this.options=res.data;
                    this.param.title='';
                    this.value='';
                    this.param.content='';
                });
            },
            onSubmit() {
                let user = JSON.parse(localStorage.getItem('user'));
                this.param.value=this.value;
                addMsg(JSON.stringify({'user':user.userid,'friend':this.param.value,'sender':user.username,'title':this.param.title,'content':this.param.content})).then(res =>{
                    console.log(res);
                    if (res.state==2000){
                        this.$message.success('提交成功！');
                        location.reload();
                    }else{
                        this.$message.error(res.message);
                    }
                })
            }
        }
    };
</script>
